package obj;

import data.DataHelper;

import java.io.*;
import java.util.*;

public class Accum {
    public static final String NA = "уточняйте";

    public static final String ACCUMS_PATH = "data/accums/accum.%s.txt";
    public static final String ACCUM_PATH = "data/accums/";
    public static final String BASE_URL = "http://extramobi.ru/product/accumulator_";
    public static final String PHOTO = "%s-medium.jpg,%s-small.jpg,%s-large.jpg";

    public String name;
    public String brandName;
    public String brandShort;
    public String modelName;
    public String vendor = NA;
    public String model = NA;
    public String power = NA;
    public String type = NA;
    public String volt = NA;
    public int price;
    public int oldPrice;
    public int count;
    public String[] devices;
    public List<String> deviceURLs;
    public String photo;

    public String url;
    public String art = "";
    public String urlclear;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Accum accum = (Accum) o;

        if (name != null ? !name.equals(accum.name) : accum.name != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return name != null ? name.hashCode() : 0;
    }

    public static Accum parseAccum(String line, double coef) {
        Accum accum = new Accum();
        String[] fields = line.split(";");
        accum.name = fields[0];
//        String[] nameFields = accum.name.split(" ");
//        if (nameFields.length == 2) {
//            accum.brandName = nameFields[0];
//        } else if (nameFields.length == 3) {
//            accum.brandName = nameFields[0] + " " + nameFields[1];
//        }
        if (fields.length == 11) {
            accum.vendor = fields[2];
            accum.model = fields[3];
            accum.power = fields[4];
            accum.type = fields[5];
            accum.volt = fields[6];
            accum.price = Integer.valueOf(fields[7]);
            accum.oldPrice = Integer.valueOf(fields[8]);
            accum.count = Integer.valueOf(fields[9].trim());
            accum.art = fields[10].trim();
        } else if (fields.length > 4) {
            accum.vendor = fields[2];
            accum.model = fields[3];
            accum.power = fields[4];
            accum.type = fields[5];
            accum.volt = fields[6];
            accum.price = Integer.valueOf(fields[7]);
            accum.count = Integer.valueOf(fields[8]);
            accum.oldPrice = (int) (accum.price * coef);
        } else {
            accum.price = Integer.valueOf(fields[2]);
            accum.count = Integer.valueOf(fields[3]);
            accum.oldPrice = (int) (accum.price * coef);
        }
        accum.brandName = accum.vendor;
        accum.brandShort = getShortBrand(accum.vendor);
        accum.devices = fields[1].split(",");
        accum.url = DataHelper.codeInURL(BASE_URL + accum.name).toLowerCase();
        accum.urlclear = DataHelper.codeInURL(accum.name).toLowerCase();
        accum.deviceURLs = new ArrayList<String>();
        for (String model : accum.devices) {
            if (model.contains("lx570")) {
                System.out.println();
            }
            String deviceUrl = DataHelper.codeInURL(BASE_URL + accum.brandName + "_" + model);
            accum.deviceURLs.add(deviceUrl);
        }
        accum.photo = String.format(PHOTO, accum.name, accum.name, accum.name);
        return accum;
    }

    private static String getShortBrand(String vendor) {
        if (vendor.equalsIgnoreCase("sony ericsson")) {
            return "SE";
        }
        return vendor.toUpperCase().charAt(0) + "";
    }

    public static Accum parseAccum(String line, double coef, int lineNumber) {
        try {
            return parseAccum(line, coef);
        } catch (Exception e) {
            throw new RuntimeException("Line: " + lineNumber + " :", e);
        }
    }

    public static List<Accum> parseAccums() throws IOException {
        String[] brands = new File(ACCUM_PATH).list();
        List<Accum> accums = new ArrayList<Accum>();
        for (String brandFile : brands) {
            if (!brandFile.endsWith(".txt") || brandFile.contains("maxcell") || brandFile.contains("craftmann")) {
                continue;
            }
            String brandName = brandFile.split("\\.")[1];
            accums.addAll(parseAccums(brandName, 1));
        }
        return accums;
    }

    public static Map<String, List<String>> parseModels() throws IOException {
        Map<String, List<String>> modelsPerBrand = new HashMap<String, List<String>>();
        for (Accum accum : parseAccums()) {
            List<String> models;
            if (modelsPerBrand.containsKey(accum.brandName)) {
                models = modelsPerBrand.get(accum.brandName);
                models = new ArrayList<String>(models);
                models.addAll(Arrays.asList(accum.devices));
            } else {
                models = Arrays.asList(accum.devices);
            }
            modelsPerBrand.put(accum.brandName, models);
        }
        return modelsPerBrand;
    }

    public static Map<String, List<String>> parseUrls() throws IOException {
        Map<String, List<String>> urlsPerBrand = new HashMap<String, List<String>>();
        for (Accum accum : parseAccums()) {
            List<String> urls;
            if (urlsPerBrand.containsKey(accum.brandName)) {
                urls = urlsPerBrand.get(accum.brandName);
                urls.addAll(accum.deviceURLs);
            } else {
                urls = accum.deviceURLs;
            }
            urlsPerBrand.put(accum.brandName, urls);
        }
        return urlsPerBrand;
    }

    public static List<Accum> parseAccums(String brandName, double coef) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(String.format(ACCUMS_PATH, brandName.replace("Sony Ericsson", "SE"))));
        List<Accum> accumsList = new ArrayList<Accum>();
        String line;
        int num = 0;
        while ((line = reader.readLine()) != null) {
            if (line.contains("﻿")) {
                line = line.replace("﻿", "");
            }
            if (line.trim().isEmpty()) {
                continue;
            }
            Accum accum = Accum.parseAccum(line, coef, ++num);
            accumsList.add(accum);
        }
        reader.close();
        return accumsList;
    }
}
